#include "stdafx.h"
#include "wf_drawlinetangent2.h"

#include "pf_creation.h"
#include "pf_snapper.h"



WF_DrawLineTangent2::WF_DrawLineTangent2(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw Tangents 2", container, graphicView) {

    tangent = NULL;
    circle1 = NULL;
    circle2 = NULL;
}

QAction* WF_DrawLineTangent2::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Tan&gent (C,C)"),
    QAction* action = new QAction(tr("Tangent (C,C)"), NULL);
	action->setIcon(QIcon(":/extui/linestan2.png"));
    //action->zetStatusTip(tr("Draw tangent (circle, circle)"));

    return action;
}


void WF_DrawLineTangent2::trigger() {
    PF_PreviewActionInterface::trigger();

    if (tangent!=NULL) {
        PF_Entity* newEntity = NULL;

        newEntity = new PF_Line(container,
                                tangent->getData());

        if (newEntity!=NULL) {
            newEntity->setLayerToActive();
            newEntity->setPenToActive();
            container->addEntity(newEntity);

            // upd. undo list:
            if (document!=NULL) {
                document->startUndoCycle();
                document->addUndoable(newEntity);
                document->endUndoCycle();
            }
			graphicView->redraw(LX_Define::RedrawDrawing);
            setStatus(SetCircle1);
        }
        delete tangent;
        tangent = NULL;
    } else {
        PF_DEBUG->print("PF_ActionDrawLineTangent2::trigger:"
                        " Entity is NULL\n");
    }
}



void WF_DrawLineTangent2::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawLineTangent2::mouseMoveEvent begin");

    PF_Vector mouse(graphicView->toGraphX(e->x()),
                    graphicView->toGraphY(e->y()));

    switch (getStatus()) {
    case SetCircle1: {
            PF_Entity* en = catchEntity(e, LX_Define::ResolveAll);
            if (en!=NULL && (en->rtti()==LX_Define::EntityCircle ||
                             en->rtti()==LX_Define::EntityArc)) {
                circle1 = en;
            }
        }
        break;

    case SetCircle2: {
            PF_Entity* en = catchEntity(e, LX_Define::ResolveAll);
            if (en!=NULL && (en->rtti()==LX_Define::EntityCircle ||
                             en->rtti()==LX_Define::EntityArc)) {
                circle2 = en;

                PF_Creation creation(NULL, NULL);
                PF_Line* t = creation.createTangent2(mouse,
                                                     circle1,
                                                     circle2);

                if (t!=NULL) {
                    if (tangent!=NULL) {
                        delete tangent;
                    }
                    tangent = (PF_Line*)t->clone();

                    deletePreview();
                    preview->addEntity(t);
                    drawPreview();
                }
            }
        }
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionDrawLineTangent2::mouseMoveEvent end");
}



void WF_DrawLineTangent2::mouseReleaseEvent(PF_MouseEvent* e) {

    if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    } else {
        switch (getStatus()) {
        case SetCircle1:
            setStatus(SetCircle2);
            break;

        case SetCircle2:
            trigger();
            break;
        }
    }

}



void WF_DrawLineTangent2::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetCircle1:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Select first circle or arc"),
                                                tr("Cancel"));
            break;
        case SetCircle2:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Select second circle or arc"),
                                                tr("Back"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DrawLineTangent2::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawLineTangent2::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
    }
}



// EOF
